<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//for stock
//ini_set("display_errors",1);
//error_reporting(E_ALL);
$terminal = '412';
echo '<pre>';

function group_assoc($array, $key) {
    $return = array();
    foreach($array as $v) {
        $return[$v[$key]][] = $v;
    }
    return $return;
}

function toArray($obj)
{
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }
    return $new;
}

require_once('simplesql.class.php5');

$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}

$db = new simplesql($hostName,$userName,$password,$dbName);
$db->query("SET NAMES utf8");

	$myarticles = file_get_contents('cache.txt');
	$objInfo = unserialize($myarticles);
	$arrInfo = toArray($objInfo);	
	//Group the requests by their account_id
	
	//get data group by SupplierName
	$new_list = group_assoc($arrInfo, 'ItemGroup');
	$arr_category = array();
	foreach($new_list as $key => $value){
		$itemgroup = $key;
		$category_name = $value[0]['ItemGroupName'];
		$arr_category[$key]=$category_name;
	}
	
	//var_dump($arr_category);
	
	$action = isset($_GET['action'])?$_GET['action']:"insert";
	$error = false;
	$db->query("BEGIN");
	
	foreach($arr_category as $key=>$value){
		$cat = $db->fetch_row('itemgroup = ' . $key,'category', FETCH_ASSOC);
		
		$exists = $db->get_rows();
		if(empty($exists)){
			//check terminal category exists
			
			$db->fetch_row('category_id = ' . $terminal, 'category');
			$exists1 = $db->get_rows();		
			if($exists1){
				
				$fields = array('parent_id','itemgroup','date_added','status');
				$data = array($terminal, $key, date('Y-m-d'), '0'); // add to parent category Terminal ID = 412				
				$category_id = $db->insert($fields, $data, 'category');
				
				if(!$category_id){
					$error = true;
					break;
				}
				



				
				$fields = array('category_id', 'language_id',  'name');
				$data = array($category_id, '1', $value);
				$db->insert($fields, $data, 'category_description');

				$fields = array('category_id', 'store_id');
				$data = array($category_id, '0');
				$db->insert($fields, $data, 'category_to_store');

			}
			
		}else{
			$fields = array('name');
			$data = array($value);
			
			$category_id = $cat[0]['category_id'];
			$result = $db->update($fields, $data, 'category_id = ' . $category_id, 'category_description');
			
			if(!$result){
				$error = true;
				break;
			}
		
		}
	}
			
			
	
	if(!$error)  
	{  
	//*** Commit Transaction ***//  
		$db->query("COMMIT");  
		echo "OK <br />";  
		
	}  
	else  
	{  
		//*** RollBack Transaction ***//  
		$db->query("ROLLBACK");  
		echo "Error:  [".$db->get_lastquery()."]" ."-" . mysql_error();  
	}
	echo '</pre>';


?>
</body>
</html>